package cn.lili.modules.goods.mapper;

import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsHotPopularDTO;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
import cn.lili.modules.goods.entity.vos.GoodsVO;
import cn.lili.modules.operationcenter.entity.dto.OperationCenterSupplierDTO;
import cn.lili.modules.operationcenter.entity.vos.OperationCenterSupplierVo;
import cn.lili.modules.order.order.entity.dos.Order;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 规格项数据处理层
 *
 * @author pikachu
 * @since 2020-02-18 15:18:56
 */
public interface GoodsMapper extends BaseMapper<Goods> {

    /**
     * 根据店铺ID获取商品ID列表
     *
     * @param storeId 店铺ID
     * @return 商品ID列表
     */
    @Select("SELECT id FROM li_goods WHERE store_id = #{storeId}")
    List<String> getGoodsIdByStoreId(String storeId);

    /**
     * 添加商品评价数量
     *
     * @param commentNum 评价数量
     * @param goodsId    商品ID
     */
    @Update("UPDATE li_goods SET comment_num = comment_num + #{commentNum} WHERE id = #{goodsId}")
    void addGoodsCommentNum(Integer commentNum, String goodsId);

    /**
     * 查询商品VO分页
     *
     * @param page         分页
     * @param queryWrapper 查询条件
     * @return 商品VO分页
     */
    @Select("select g.* from li_goods as g ")
    Page<GoodsVO> queryByParams(Page<GoodsVO> page, @Param(Constants.WRAPPER) Wrapper<GoodsVO> queryWrapper);

    /**
     * 根据商品id获取全部skuId的集合
     *
     * @param queryWrapper 查询参数
     * @return 全部skuId的集合
     */
    @Select("SELECT id FROM li_goods ${ew.customSqlSegment}")
    List<String> getGoodsSkuIdByGoodsId(@Param(Constants.WRAPPER) Wrapper<Goods> queryWrapper);

    /**
     * 根据商品id获取全部skuId的集合
     *
     * @param queryWrapper 查询参数
     * @return 全部skuId的集合
     */
    @Select("SELECT store_id FROM li_goods ${ew.customSqlSegment}")
    List<String> getGoodsSkuStoreIdByGoodsId(@Param(Constants.WRAPPER) Wrapper<Goods> queryWrapper);

    void saveGoodsList(@Param("goodsList") List<Goods> goodsList);

    @Select("select third_party_platform_id from li_goods where third_party_platform_type = 1 and delete_flag = 0 order by third_party_platform_id+0 desc limit 1")
    Integer selectMaxId();

    Page<Goods> pageVo(Page<Goods> objectPage, @Param("ew") QueryWrapper queryWrapper, String goodsName);

    /**
     * 店铺，批量添加分类
     *
     * @param ids 商品id
     * @param categoryPathSon 分类id
     * @param categoryPathSonName 分类名称
     */
    void updateCategoryById(@Param("ids") List<String> ids, @Param("categoryPathSon") String categoryPathSon,@Param("categoryPathSonName") String categoryPathSonName);

    /**
     * 供应商，批量添加分类
     *
     * @param ids 商品id
     * @param categoryPathSon 分类id
     * @param categoryPathSonName 分类名称
     */
    void updateCategoryByIdOrSupplierId(@Param("ids") List<String> ids, @Param("categoryPathSon") String categoryPathSon,@Param("categoryPathSonName") String categoryPathSonName);


    @Select("select goods_id,third_party_platform_id, count(third_party_platform_id) cc  from li_goods_sku where  third_party_platform_id is not null and store_id = 1713890839052210178 group by third_party_platform_id having cc > 1")
    List<GoodsSku> sele();

    void replaceCategoryById(@Param("ids") List<String> ids,@Param("categoryPathSon") String categoryPathSon,@Param("categoryPathSonName") String categoryPathSonName);


    void replaceCategoryByIdOrSupplierId(@Param("ids") List<String> ids,@Param("categoryPathSon") String categoryPathSon,@Param("categoryPathSonName") String categoryPathSonName);


    @Select("<script>" +
            "SELECT g.* FROM li_goods AS g " +
            "LEFT JOIN li_goods_hot_popular AS hot ON g.id = hot.goods_id AND hot.delete_flag = 0 " +
            "WHERE hot.goods_id is null AND g.scene in ('MEMBER', 'STORE', 'MANAGER') " +
            "AND g.market_enable = 'UPPER' AND g.delete_flag = 0 AND g.auth_flag = 'PASS'" +
            "<if test='dto.goodsName != null and dto.goodsName != \"\"'>" +
            " AND g.goods_name LIKE CONCAT(#{dto.goodsName}, '%') " +
            "</if> " +
            "<if test='dto.categoryPath != null and dto.categoryPath != \"\"'>" +
            " AND g.category_path LIKE CONCAT('%',#{dto.categoryPath}, '%') " +
            "</if>" +
            "</script>")
    Page<Goods> selectGoodsListPaging(Page page, @Param("dto") GoodsHotPopularDTO dto);

    Long getCurrentMonthGoodsCountByStoreIds(@Param("storeIds") List<String> storeIds);


    /**
     * 运营中心-服务商-供货商家列表
     * @param page
     * @param dto
     * @return
     */
//    @Select("<script> SELECT " +
//            " us.province_id, " +
//            "s.id AS supplierId, " +
//            "s.store_name AS supplierName, " +
//            "us.id AS urbanId," +
//            "us.urban_name AS urbanName, " +
//            "s.create_time AS createTime, " +
//            "( SELECT sum( 1 ) FROM lilishop_goods.li_goods AS g WHERE g.store_id = s.id AND g.delete_flag = 0 ) AS goodsCount, " +
//            "(SELECT sum(1)  FROM lilishop_goods.li_goods AS g WHERE g.store_id = s.id  AND g.delete_flag = 0  AND g.create_time >= #{dto.startTime} " +
//            " AND g.create_time < #{dto.endTime}) AS goodsCountByMonth, " +
//            " (SELECT sum( 1 ) FROM lilishop_goods.li_goods_down_record AS gdr WHERE gdr.supplier_id = s.id ) AS downGoodsCount, " +
//            " (SELECT\tsum( 1 ) \tFROM lilishop_goods.li_goods_down_record AS gdr WHERE gdr.supplier_id = s.id AND gdr.create_time >= #{dto.startTime} " +
//            " AND gdr.create_time < #{dto.endTime}) AS downGoodsCountByMonth " +
//            " FROM lilishop_goods.li_store AS s" +
//            " JOIN lilishop_urban_substation.li_urban_substation AS us ON s.urban_user_id = us.user_id AND us.delete_flag = 0 " +
//            " WHERE us.province_id = #{dto.provinceId}   <if test= \"dto.supplierName!=null and  dto.supplierName!=''\">"
//            +" AND s.store_name LIKE CONCAT(#{dto.supplierName},'%') </if>"
//            +" <if test= \"dto.urbanId!=null and dto.urbanId!='' \">"
//            +" AND us.id = #{dto.urbanId} </if> "
//            + " ORDER BY goodsCount DESC, goodsCountByMonth DESC ,downGoodsCount DESC "
//            + " </script>")
    Page<OperationCenterSupplierVo> getSupplierByPage(Page page, @Param("dto") OperationCenterSupplierDTO dto);

    List<Order> sycnFreeOrder();


    /**
     * 免税商品-获取免税商品列表>: 热销商品,新品商品
     */
    Page<GoodsVO> getFreeTaxGoods(@Param("params") GoodsSearchParams params, Page page);

}